package com.chixing.service.impl;

import com.chixing.pojo.Item;
import com.chixing.pojo.ItemPlan;
import com.chixing.service.ElasticSearchService;
import com.chixing.service.PlanRepository;
import com.chixing.service.TravelSightsRepository;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.*;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class ElasticSearchServiceImpl implements ElasticSearchService {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private TravelSightsRepository travelSightsRepository;
    @Autowired
    private PlanRepository planRepository;

    @Override
    public ServerResponse getByElasticSearchSights(Float minPrice, Float maxPrice, Integer level, String type, String keyword, Integer pageNum) {
        PageRequest pageRequest = PageRequest.of(pageNum - 1, 3);
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (minPrice != 1) {
            QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("travel_tickets_price").gte(minPrice);
            boolQueryBuilder.filter(queryBuilder1);
        }
        if (maxPrice != 1) {
            QueryBuilder queryBuilder2 = QueryBuilders.rangeQuery("travel_tickets_price").lte(maxPrice);
            boolQueryBuilder.filter(queryBuilder2);
        }
        if (level != 0) {
            QueryBuilder queryBuilder3 = QueryBuilders.matchQuery("travel_sights_level", level);
            boolQueryBuilder.filter(queryBuilder3);
        }
        if (!type.equals("1")) {
            QueryBuilder queryBuilder4 = QueryBuilders.matchQuery("travel_sights_type", type);
            boolQueryBuilder.filter(queryBuilder4);
        }
        if (!keyword.equals("")) {
            NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                    .withQuery(
                            boolQueryBuilder
                                    .must(QueryBuilders.matchQuery("travel_sights_loc_city", keyword))
                    )
                    .withPageable(pageRequest)
                    .build();
            SearchHits<Item> search = elasticsearchRestTemplate.search(nativeSearchQuery, Item.class);

            return ServerResponse.getSuccess(search);
        }else {
            NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                    .withPageable(pageRequest)
                    .build();
            SearchHits<Item> search = elasticsearchRestTemplate.search(nativeSearchQuery, Item.class);
            return ServerResponse.getSuccess(search);
        }
    }

    @Override
    public ServerResponse getByElasticSearchPlan(Float minPrice, Float maxPrice, Integer perNum, Integer days, String keyword, Integer pageNum) {
        PageRequest pageRequest = PageRequest.of(pageNum - 1, 3);
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (minPrice!=-1){
            QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("itineraryBudget").gte(minPrice);
            boolQueryBuilder.filter(queryBuilder1);
        }
        if(maxPrice!=-1){
            QueryBuilder queryBuilder2 = QueryBuilders.rangeQuery("itineraryBudget").lte(maxPrice);
            boolQueryBuilder.filter(queryBuilder2);
        }
        if(perNum!=-1){
            QueryBuilder queryBuilder3 = QueryBuilders.matchQuery("numberPerson", perNum);
            boolQueryBuilder.filter(queryBuilder3);
        }
        if(days!=-1){
            QueryBuilder queryBuilder4 = QueryBuilders.matchQuery("itineraryTravelDays", days);
            boolQueryBuilder.filter(queryBuilder4);
        }
        if (!keyword.equals("")) {
            NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                    .withQuery(
                            boolQueryBuilder
                                    .must(QueryBuilders.matchQuery("itineraryAdd", keyword))
//                                    .filter(QueryBuilders.matchQuery("travelSightsName", keyword))
//                                    .filter(QueryBuilders.matchQuery("travelSightsType", keyword))
                    )
//                    .withSort(SortBuilders.fieldSort("travelSightsScore").order(SortOrder.DESC))
                    .withPageable(pageRequest)
                    .build();
            SearchHits<ItemPlan> search = elasticsearchRestTemplate.search(nativeSearchQuery, ItemPlan.class);

            return ServerResponse.getSuccess(search);
        }else {
            NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                    .withPageable(pageRequest)
                    .build();
            SearchHits<ItemPlan> search = elasticsearchRestTemplate.search(nativeSearchQuery, ItemPlan.class);
            return ServerResponse.getSuccess(search);
        }
    }
}
